package com.qgqaqgqa.deviceinfomanager.view;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.qgqaqgqa.deviceinfomanager.R;
import com.qgqaqgqa.deviceinfomanager.dialog.DialogUtils;
import com.qgqaqgqa.deviceinfomanager.finals.AppConfig;
import com.qgqaqgqa.deviceinfomanager.util.utilcode.util.SPUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Optional;

/**
 * 标题栏
 * User: Created by 钱昱凯
 * Date: 2018/3/8 0008
 * Time: 11:29
 * Email:342744291@qq.com
 */
public class CommonSearchPage implements View.OnClickListener {
    private EditText etSearch;
    private Activity mActivity;

    @BindView(R.id.ll_search)
    View llSearch;

    @BindView(R.id.tv_job_type)
    TextView mTvJobType;//作业类型
    @BindView(R.id.tv_kssj)
    TextView mTvKssj;//开始时间
    @BindView(R.id.tv_jssj)
    TextView mTvJssj;//结束时间

    @BindView(R.id.et_job_type)
    TextView mEtJobType;//作业类型
    @BindView(R.id.et_kssj)
    TextView mEtKssj;//开始时间
    @BindView(R.id.et_jssj)
    TextView mEtJssj;//结束时间

    @BindView(R.id.btn_search_cancel)
    View btnSearchCancel;
    @BindView(R.id.btn_search_ok)
    View btnSearchOk;

    String[] jobTypes = new String[]{"全部","抢修作业", "农网工程"};

    private Dialog jobTypeDialog;
    public int job_type=-1;
    private Dialog kssjDialog;
    private Dialog jssjDialog;
    public CommonSearchPage(Activity activity) {
        this(activity,null);
    }

    public CommonSearchPage(Activity activity, EditText etSearch) {
        this.mActivity = activity;
        ButterKnife.bind(this, activity);

//        btnSearchOk.setVisibility(View.VISIBLE);
        if(etSearch!=null){
            this.etSearch=etSearch;
//            btnSearchOk.setVisibility(View.GONE);
        }

        hideJobType();
        switch (SPUtils.getInstance().getString(AppConfig.SP_USER_POST)){
            case "yjry"://运检人员
            case "ddry"://调度人员
            case "qxfzr"://抢修负责人
            case "qxry"://抢修人员
                job_type=0;
                break;

            case "jlry"://监理人员
            case "yzry"://业主人员
            case "sgfzr"://施工负责人
            case "sgry"://施工人员
                job_type=1;
                break;
            default:
                showJobType();
                break;
        }
    }

    public void hideJobType() {
        ((View)mEtJobType.getParent()).setVisibility(View.GONE);
    }
    public void showJobType() {
        ((View)mEtJobType.getParent()).setVisibility(View.VISIBLE);
    }

    @Optional
    @OnClick({R.id.et_job_type,R.id.et_kssj, R.id.et_jssj,R.id.btn_search_cancel})
    @Override
    public void onClick(View v) {
        HashMap<String, String> map = new HashMap<>();
        switch (v.getId()) {
            case R.id.et_job_type:
                showJobTypeSelect();
                break;
            case R.id.et_kssj:
                showKssjSelect();
                break;
            case R.id.et_jssj:
                showKJssjSelect();
                break;
            case R.id.btn_search_cancel:
                hideSearch();
                break;
        }
    }
    public void setSearch(View.OnClickListener listener){
        btnSearchOk.setVisibility(View.VISIBLE);
        btnSearchOk.setOnClickListener(listener);
    }

    public void hideSearch() {
        if(etSearch!=null){
            etSearch.setFocusableInTouchMode(false);
            etSearch.clearFocus();
        }
        llSearch.setVisibility(View.GONE);
    }

    public void showSearch() {
        if(etSearch!=null){
            etSearch.setFocusableInTouchMode(true);
            etSearch.requestFocus();
        }
        llSearch.setVisibility(View.VISIBLE);
    }

    public String getJob_type() {
        return job_type==-1?"":job_type+"";
    }

    public void setJob_type(int job_type) {
        this.job_type = job_type;
    }

    public String getKssj() {
        return mEtKssj.getText().toString().trim();
    }
    public String getJssj() {
        return mEtJssj.getText().toString().trim();
    }

    private void showJobTypeSelect() {
        if (jobTypeDialog == null) {
            jobTypeDialog = DialogUtils.getSingleListDialog(mActivity, jobTypes, job_type, (dialog, which) -> {
                mEtJobType.setText(jobTypes[which]);
                job_type = which-1;
                dialog.dismiss();
            });
        }
        jobTypeDialog.show();
    }

    private void showKssjSelect() {
        if (kssjDialog == null) {
            kssjDialog=DialogUtils.getDateAndTimeDialog(mActivity,mEtKssj);
        }
        kssjDialog.show();
    }
    private void showKJssjSelect() {
        if (jssjDialog == null) {
            jssjDialog=DialogUtils.getDateAndTimeDialog(mActivity,mEtJssj);
        }
        jssjDialog.show();
    }
}